<script setup>

import {
  CaretBottom,
  Crop,
  EditPen, Goods,
  Management,
  Promotion,
  SwitchButton,
  User,
  UserFilled,
  Plus,
  Refresh,
  Unlock,
  Message,
  Location,
  Calendar,
  Search, Paperclip, Picture,
  Star,
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { useAccountStore } from "@/stores/accountInfo";

const account = useAccountStore();

const router = useRouter();
const handleCommand = (command) => {
  if (command === 'logout') {
    ElMessageBox.confirm(
      '你确认要退出吗？',
      '温馨提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }
    ).then(
      async () => {
        // clear data in pinia
        account.clearAll()
        ElMessage.success("退出成功")
        await router.push('/login')
      }
    )
  } else {
    router.push('/user/' + command)
  }
}
</script>

<template>
  <el-container class="layout-container">
    <!-- 左侧菜单 -->
    <el-aside width="200px">
      <div class="el-aside__logo"></div>
      <el-menu active-text-color="#ffd04b" background-color="#232323" text-color="#fff" router>

        <el-menu-item index="/DashBoard">
          <el-icon>
            <Star />
          </el-icon>
          <span>首页</span>
        </el-menu-item>

        <el-menu-item index="/admin/list" v-if="account.currentRole === 'admin'">
          <el-icon>
            <Promotion />
          </el-icon>
          <span>管理员管理</span>
        </el-menu-item>

        <el-menu-item index="/user/list" v-if="account.currentRole === 'admin'">
          <el-icon>
            <Paperclip />
          </el-icon>
          <span>用户管理</span>
        </el-menu-item>

        <el-menu-item index="/banner" v-if="account.currentRole === 'admin'">
          <el-icon>
            <Picture />
          </el-icon>
          <span>版面管理</span>
        </el-menu-item>

        <el-menu-item index="/news">
          <el-icon>
            <Message />
          </el-icon>
          <span>新闻查看</span>
        </el-menu-item>

        <el-menu-item index="/user/doorplateApply">
          <el-icon>
            <Plus />
          </el-icon>
          <span>门牌申请</span>
        </el-menu-item>

        <el-menu-item index="/user/doorplateApproval" v-if="account.currentRole == 'admin'">
          <el-icon>
            <Unlock />
          </el-icon>
          <span>门牌审批</span>
        </el-menu-item>

        <el-menu-item index="/user/AdministrativeDivisions">
          <el-icon>
            <Location />
          </el-icon>
          <span>行政区划</span>
        </el-menu-item>

        <el-menu-item index="/dashboard/doorplate-query">
          <el-icon>
            <Search />
          </el-icon>
          <span>门牌查找</span>
        </el-menu-item>

        <el-menu-item index="/user/SystemLog" v-if="account.currentRole === 'admin'">
          <el-icon>
            <Calendar />
          </el-icon>
          <span>系统日志</span>
        </el-menu-item>

        <el-sub-menu>
          <template #title>
            <el-icon>
              <UserFilled />
            </el-icon>
            <span>个人中心</span>
          </template>

          <el-menu-item index="/user/info">
            <el-icon>
              <User />
            </el-icon>
            <span>基本资料</span>
          </el-menu-item>
          <el-menu-item index="/user/changePassword">
            <el-icon>
              <EditPen />
            </el-icon>
            <span>修改密码</span>
          </el-menu-item>
          <el-menu-item index="/user/resetPassword">
            <el-icon>
              <Refresh />
            </el-icon>
            <span>重置密码</span>
          </el-menu-item>
        </el-sub-menu>

      </el-menu>
    </el-aside>
    <!-- 右侧主区域 -->
    <el-container>
      <!-- 头部区域 -->
      <el-header>
        <div>欢迎，<strong>{{ account.currentInfo.name }}</strong></div>
        <el-dropdown placement="bottom-end" @command="handleCommand">
          <span class="el-dropdown__box">
            <el-avatar :src="account.currentInfo.avatar ? account.currentInfo.avatar : avatar" />
            <el-icon>
              <CaretBottom />
            </el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="info" :icon="User">基本信息</el-dropdown-item>
              <el-dropdown-item command="changePassword" :icon="EditPen">修改密码</el-dropdown-item>
              <el-dropdown-item command="resetPassword" :icon="EditPen">重置密码</el-dropdown-item>
              <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-header>
      <!-- 中间区域 -->
      <el-main>
        <router-view />
      </el-main>
      <!-- 底部区域 -->
      <el-footer>地名地址信息管理系统 ©2025 </el-footer>
    </el-container>
  </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
  height: 100vh;

  .el-aside {
    background-color: #232323;

    &__logo {
      height: 120px;
      background: url('@/assets/login_title.png') no-repeat center / 120px auto;
    }

    .el-menu {
      border-right: none;
    }
  }

  .el-header {
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .el-dropdown__box {
      display: flex;
      align-items: center;

      .el-icon {
        color: #999;
        margin-left: 10px;
      }

      &:active,
      &:focus {
        outline: none;
      }
    }
  }

  .el-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    color: #666;
  }
}
</style>